import { getHostName, type ConfigJsonType } from "~/utils";

/** 获取计算后的全局数据源 */
export const useConfigStore = () => {
  const config = useState<ConfigJsonType>("config", () => {
    return {};
  });
  const hostName = getHostName();

  /** 初始化config数据源 */
  const initConfig = async () => {
    const { data } = await useFetch<ConfigJsonType>("/api/getCommonConfig", {
      method: "POST",
      body: { hostName },
    });
    config.value = data.value || {};
    return data.value;
  };

  return {
    hostName,
    config,
    initConfig,
  };
};
